# GUI entry point
import tkinter as tk
from src.core import items as items_core
from src.storage import file_manager
from src.ui.tkinter.screens.launch_screen import show_launch_screen
import os
import threading
import urllib.request

def main():
    item_manager = items_core.create_state()
    data_path = os.path.join(os.path.dirname(__file__), 'data', 'items.json')
    if os.path.exists(data_path):
        file_manager.load_items(item_manager, data_path)
    
    root = tk.Tk()
    root.title("Croissant Bakery")

    root.geometry("1024x768")
    root.resizable(True, True)

    def play_music_loop():
        try:
            import pygame
            pygame.mixer.init()
            assets_dir = os.path.join(os.path.dirname(__file__), 'assets')
            os.makedirs(assets_dir, exist_ok=True)
            music_path = os.path.join(assets_dir, 'jazz.mp3')
            if not os.path.exists(music_path):
                try:
                    url = 'https://cdn.pixabay.com/download/audio/2022/03/15/audio_2f5a3d2e01.mp3?filename=jazz-comedy-11254.mp3'
                    urllib.request.urlretrieve(url, music_path)
                except Exception:
                    pass
            if os.path.exists(music_path):
                try:
                    pygame.mixer.music.load(music_path)
                    pygame.mixer.music.set_volume(1)
                    pygame.mixer.music.play(-1)
                except Exception:
                    pass
        except Exception:
            pass
        return None
    threading.Thread(target=play_music_loop, daemon=True).start()
    
    show_launch_screen(root, item_manager)
    
    root.mainloop()

    file_manager.save_items(item_manager, data_path)

if __name__ == "__main__":
    main()

